package org.lq.dlz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lq.dlz.common.Const;
import org.lq.dlz.common.Const.ProductStatusEnum;
import org.lq.dlz.common.Page;
import org.lq.dlz.dao.CategoryDao;
import org.lq.dlz.dao.ProductDao;
import org.lq.dlz.dao.impl.CategoryDaoImpl;
import org.lq.dlz.dao.impl.ProductDaoImpl;
import org.lq.dlz.entity.Category;
import org.lq.dlz.entity.Product;
import org.lq.dlz.query.ProductQuery;
import org.lq.dlz.service.ProductService;

import com.google.gson.Gson;
/**
 * 
 * @author 于通
 * @date   2019年9月7日
*
 */
public class ProductServiceImpl implements ProductService {
ProductDao pd = new ProductDaoImpl();
CategoryDao cg = new CategoryDaoImpl();
	
	
	@Override
	public boolean saveProduct(Product p) {
		return pd.addProduct(p)>0;
	}

	@Override
	public boolean updateProduct(Product p) {
		return pd.updateProduct(p)>0;
	}

	@Override
	public Product getProductById(int id) {
		return pd.getProductById(id);
	}

	@Override
	public List<Product> findPage(Page<Product> page) {
		return pd.findPage(page);
	}

	@Override
	public List<Product> getProductByCategoryId(int cid, Page<Product> page) {
		return pd.getProductByCategoryId(cid, page);
	}

	@Override
	public int totalCount() {
		// TODO Auto-generated method stub
		return pd.totalCount();
	}

	@Override
	public int totalCount(ProductQuery query) {
		return pd.totalCount(query);
	}

	@Override
	public String findPage(Page<Product> page, ProductQuery query) {
		Map<String, Object> map = new HashMap<String, Object>();
		pd.findPage(page, query);
		for (Product product : page.getList()) {
			product.setStatus_name(Const.ProductStatusEnum.getProductStatusEnum(product.getStatus()).getValue());
			Category category = cg.findById(product.getCategory_id());
			product.setCategory(category);
		}
		map.put("code", 0);
		map.put("msg","");
		map.put("count", totalCount(query));
		map.put("data", page.getList());
		return new Gson().toJson(map);
	}

	@Override
	public boolean updateStatus(int id, int status) {
		// TODO Auto-generated method stub
		return pd.updateStatus(id, status)>0;
	}

	@Override
	public List<Product> getHotProduct() {
		// TODO Auto-generated method stub
		return pd.getHOTProduct(ProductStatusEnum.HOT_SALE);
	}

	@Override
	public void findProductByCid(Page<Product> page, ProductQuery query) {
		// TODO Auto-generated method stub
			pd.findProductByCid(page, query);
	}

	@Override
	public List<Product> findCartProduct(int... id) {
		// TODO Auto-generated method stub
		return pd.findCartProduct(id);
	}

}
